/**
 * <p>Title: ServiceZMCT01.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2012-6-13 上午10:00:31  高强  Create
 *</pre>
 */
package com.baosight.sczx.zm.ct.service;

import java.sql.*;  
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cl.domain.ZMCLSecurityItems;
import com.baosight.sczx.zm.ct.domain.ZMCTDataInterface;
import com.baosight.sczx.zm.domain.ZM05;


/**
 * <p>Title: ServiceZMCT01.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2012-6-13 上午10:00:31  高强  Create
 *</pre>
 */
public class ServiceZMCT01 extends ServiceEPBase {
	
	/**
	 * 页面初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		EiInfo outInfo = super.query(inInfo, "ZMCTDataInterface.query", new ZMCTDataInterface());
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		//校验名称、简称是否重复
		boolean validateValue = validate(inInfo, "ZMCTDataInterface.validate", new HashMap<String, String>());
		
		if(validateValue) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CT_DATAINTERFACE"));//主键
			EiInfo outinfo = super.insert(inInfo, "ZMCTDataInterface.insert");
			return outinfo;
		}
	}
	
	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2012-6-13 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		//校验名称、简称是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		
		boolean validateValue = validate(inInfo, "ZMCTDataInterface.validate", params);
		if(validateValue) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.update(inInfo, "ZMCTDataInterface.update");
			return outInfo;
		}
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
		EiInfo _temp = super.delete(inInfo, "ZMCTDataInterface.delete");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 测试数据库连接是否成功
	 * <p>Function: confirm</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 上午10:00:31  高强  
	 * </p>
	 *
	 */
	public EiInfo confirm(EiInfo inInfo) 
	{
		try {
		//校验名称、简称是否重复
		String driver = inInfo.getBlock("result").getCellStr(0, "fdDriver");
		String url = inInfo.getBlock("result").getCellStr(0, "fdUrl");
		String user = inInfo.getBlock("result").getCellStr(0, "fdUserName");
		String passWord = inInfo.getBlock("result").getCellStr(0, "fdPassword");
//		装载JDBC驱动程序 
		Class.forName(driver).newInstance();   
//		连接数据库 
//		if(driver.equals("sun.jdbc.odbc.JdbcOdbcDriver"))
//		{
//			java.sql.Connection   sqlCon  = DriverManager.getConnection(url);
//		}else
//		{
			java.sql.Connection   sqlCon  = DriverManager.getConnection(url,user,passWord); 
//		}
		
		inInfo.setMsg("链接成功");
        return inInfo;
		 }catch(Exception e) {  
			  e.printStackTrace(); 
			 inInfo.setMsg("链接失败");
			 return inInfo;
	     }       
	}
	
	/**
	 * 启用某条数据
	 * <p>Function: modifyStart</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 上午10:00:31  高强  
	 * </p>
	 *
	 */
	public EiInfo modifyStart(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCTDataInterface.modifyStart");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 停用某条数据
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 上午10:00:31  高强  
	 * </p>
	 *
	 */
	public EiInfo modifyStop(EiInfo inInfo)
	{
		//校验名称、简称是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdPersonDs", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		params.put("fdDepartmentDs", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		
		boolean validateValue = canStop(inInfo, params);
		if(validateValue) {
			return inInfo;
		}
		EiInfo _temp = super.update(inInfo, "ZMCTDataInterface.modifyStop");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 *在新增修改操作时校验名称简称不能重复 
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdName = inInfo.getBlock("result").getCellStr(0, "fdName");
		String fdAbbreviation = inInfo.getBlock("result").getCellStr(0, "fdAbbreviation");
		
		params.put("fdName", fdName);
		params.put("fdAbbreviation", fdAbbreviation);
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCTDataInterface _temp = (ZMCTDataInterface)result.get(0);
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
				msg.append(" 名称 ");
			}
			if(_temp.getFdAbbreviation() != null && _temp.getFdAbbreviation().equals(fdAbbreviation)) {
				msg.append(" 简称 ");
			}
			msg.append("重复！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
	public EiInfo queryDs(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCTDataInterface.queryInterface", new ZMCTDataInterface());
		return outInfo;
	}
	
	/**
	 * 检查是否有可以停用数据源
	 * <p>Function: canStop</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-7-38下午         高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean canStop(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		Map<String, String> paramsModel =new HashMap<String, String>();
		paramsModel.put("fdDs", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		List resultModel = dao.query("ZMCTSmsModel.query", paramsModel);
		List result = dao.query("ZMCTSystem.query", params);
		if((result!=null && result.size() > 0) || (resultModel!=null && resultModel.size() > 0)) {
			inInfo.setStatus(-1);
			inInfo.setMsg("该数据源已经被使用不能停用!");
			return true;
		}
		return false;
	}
	
}

